﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace ProjectSem40701.Admin
{
    public partial class ExamAdd : System.Web.UI.Page
    {
        private const string SUCCESS = "success";
        private const string FAIL = "fail";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string strUser = System.Web.HttpContext.Current.User.Identity.Name;
                string strUserID = Membership.GetUser(strUser).ProviderUserKey.ToString();
                Guid userID = new Guid(strUserID);

                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
                DateTime dtStartTime=Convert.ToDateTime(this.txtStartTime.Text.Trim());
                DateTime dtEndTime =Convert.ToDateTime(this.txtEndTime.Text.Trim());

                AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter dpExam =
                    new AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter();

                if (this.checkTest.Checked)
                {
                    ResetIsCurrent();
                }
                if (this.cbbStatus.SelectedValue.ToString() == "1")
                {
                    ResetStatus();
                }


                int ok = dpExam.tbl_Exam_Insert(userID,
                    this.txtExamName.Text.Trim(),
                    Server.HtmlEncode(this.txtDescription.Value.Trim()),
                    dtStartTime, dtEndTime,
                    Convert.ToInt32(this.cbbStatus.SelectedValue.Trim()),
                    this.checkTest.Checked);
                if (ok > 0)
                {
                    Show_Dialog(SUCCESS);
                }
                else
                { 
                    Show_Dialog(FAIL);
                }
            }
            catch
            { 
                Show_Dialog(FAIL);
            }
        }

        /// <summary>
        /// Reset tat ca exam status ve 0
        /// </summary>
        private void ResetStatus()
        {
            try
            {
                AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter dpExam =
                    new AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter();
                int ok = dpExam.tbl_Exam_Status_Reset();
                //if (ok < 1)
                //{
                //    throw new Exception("Reset fail");
                //}
            }
            catch
            {
                //bao that bai
                throw new Exception("Reset fail");
            }
        }

        /// <summary>
        /// Reset tat ca exam iscurrent ve 0
        /// </summary>
        private void ResetIsCurrent()
        {
            try
            {
                AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter dpExam =
                    new AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter();
                int ok = dpExam.tbl_Exam_IsCurrent_Reset();
                //if (ok < 1)
                //{
                //    throw new Exception("Reset fail");
                //}
            }
            catch
            {
                //bao that bai
                throw new Exception("Reset fail");
            }
        }

        /// <summary>
        /// Close dialog
        /// </summary>
        private void Close_Dialog()
        {
            switch (this.lblSuccess.Text)
            { 
                case SUCCESS:
                    Response.Redirect("Exam.aspx");
                    break;
                case FAIL:
                    this.btnAdd_ModalPopupExtender.Enabled = false;
                    break;
                default :
                    this.btnAdd_ModalPopupExtender.Enabled = false;
                    break;
            }
        }

        /// <summary>
        /// Show dialog
        /// </summary>
        private void Show_Dialog( String Check)
        {
            
            switch (Check)
            { 
                case SUCCESS:
                    this.ltrMessage.Text = "Thành công";                    
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Thất bại";
                    break;
                default :
                    this.ltrMessage.Text = "Không thành công";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnAdd_ModalPopupExtender.Enabled = true;
            this.btnAdd_ModalPopupExtender.Show();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        protected void btnYes_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }
    }
}
